package builder 
{
	import app.textures.TextureInfo;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.HTTPStatusEvent;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import utils.ErrorsCollector;
	/**
	 * ...
	 * @author Dmitriy Gaiduk
	 */
	public class LoaderColorFromImage extends EventDispatcher
	{
		private var _loader:Loader;
		private var _textureInfo:TextureInfo;
		private var _setColor:Boolean;
		
		public function LoaderColorFromImage(textureInfo:TextureInfo, setColor:Boolean) 
		{
			_textureInfo = textureInfo;
			_setColor = setColor;
		}
		
		public function start():void
		{
			loadPicture(_textureInfo.previewURL);
		}
		
		private function loadPicture(fileName:String):void
		{
			
			_loader = new Loader();
			configureListeners(_loader.contentLoaderInfo);
			try
			{
				_loader.load(new URLRequest(fileName)); 
			}
			catch(er:Error)
			{
				handleError(er.message);
			}
			
		}
		
		private function handleError(err:String):void
		{
			ErrorsCollector.collectError("FrameItem Loader: " + err);
		}
		
		// добавляет обработчики событий
		private function configureListeners(dispatcher:IEventDispatcher):void 
		{
            dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            dispatcher.addEventListener(Event.COMPLETE, handleLoadComplete);
			dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			
           /* dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            dispatcher.addEventListener(Event.INIT, initHandler);
            dispatcher.addEventListener(Event.OPEN, openHandler);
            dispatcher.addEventListener(Event.UNLOAD, unLoadHandler);*/
        }
		
		// удаляет обработчики событий
		private function removeListeners(dispatcher:IEventDispatcher):void
		{
			dispatcher.removeEventListener(Event.COMPLETE, handleLoadComplete);
			dispatcher.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			dispatcher.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
		}
		
		private function handleLoadComplete(e:Event):void 
		{
			removeListeners(_loader.contentLoaderInfo);
			if (_setColor)
			{
				var img:DisplayObject = _loader.content;
				var bm:BitmapData = new BitmapData(img.width, img.height, false, 0xFF0000);
				bm.draw(img);
				var color:uint = bm.getPixel(1, 1);
				_textureInfo.color = color;
				//color.toString(16).toUpperCase();
			}
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		
		private function httpStatusHandler(event:HTTPStatusEvent):void 
		{
            //handleError("httpStatusHandler: " + event);
        }

        private function initHandler(event:Event):void 
		{
            //handleError("initHandler: " + event);
        }

        private function ioErrorHandler(event:IOErrorEvent):void 
		{
			//ошибка загрузки
			_textureInfo.previewURL = "empty";
			dispatchEvent(new Event(Event.CHANGE));
           // handleError("Load image error: " + event.text);
        }

        private function openHandler(event:Event):void 
		{
            //trace("openHandler: " + event);
        }

        private function progressHandler(event:ProgressEvent):void 
		{
            //trace("progressHandler: bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
        }

        private function unLoadHandler(event:Event):void 
		{
            //trace("unLoadHandler: " + event);
        }
		
	}

}